package com.first.teacher.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.first.common.pojo.EduTeacher;
import com.first.common.pojo.TbCheckTeacher;
import com.first.common.util.VerificationUtil;
import com.first.common.vo.Result;
import com.first.teacher.mapper.TbCheckTeacherMapper;
import com.first.teacher.mapper.TeacherMapper;
import com.first.teacher.service.TeacherService;
import com.first.teacher.vo.TeacherVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @program: first-project
 * @description:
 * @author: 马英贤
 * @create: 2023-05-26 16:02
 **/
@Service
public class TeacherServiceImpl implements TeacherService {
    @Resource
    private TeacherMapper teacherMapper;
    @Autowired
    private TbCheckTeacherMapper tbCheckTeacherMapper;
    @Override
    public Page<EduTeacher> findByPage(Integer currentPage, Integer pageSize, TeacherVo teacherVo) {
        Page<EduTeacher> page = new Page<>(currentPage,pageSize);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.eq("check_status",1);
        if (teacherVo.getName() != null){
            wrapper.like("name",teacherVo.getName());
        }
        if (teacherVo.getMobile() != null){
            wrapper.like("mobile",teacherVo.getMobile());
        }
        if (teacherVo.getStatus() != null){
            wrapper.eq("status",teacherVo.getStatus());
        }
        Page<EduTeacher> teacherPage = teacherMapper.findByPageAll(page,wrapper);
        return teacherPage;
    }



    @Override
    public Integer updateStatus(String id, String status) {
        EduTeacher teacher = new EduTeacher();
        teacher.setId(id);
        teacher.setStatus(status);
        teacher.setGmtModified(LocalDateTime.now());
        Integer update = teacherMapper.updateById(teacher);
        return update;
    }

    @Override
    public void update(EduTeacher eduTeacher) {
        teacherMapper.updateById(eduTeacher);
    }



    @Override
    public EduTeacher findTeacherByTeacherId(String teacherId) {
        QueryWrapper<EduTeacher> wrapper=new QueryWrapper<>();
        wrapper.eq("id",teacherId);
        return teacherMapper.selectOne(wrapper);
    }

    @Override
    public Result getYanZhengMa(String mobile) {
        String code = VerificationUtil.getCode(mobile);
        return Result.success(code);
    }

    @Override
    @Transactional
    public String increaseTeacher(EduTeacher eduTeacher) {
        String s = teacherMapper.selectTeacherId(eduTeacher.getMobile());
        if (StringUtils.hasText(s)){
            if (s.equals("2")){
                return s;
            }
            if (s.equals("1")){
                return s;
            }
        }
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.eq("name",eduTeacher.getName());
        EduTeacher eduTeacher1 = teacherMapper.selectOne(wrapper);
        if (eduTeacher1 != null){
            return "0";
        }
        String id = IdUtil.getSnowflakeNextId()+"";
        eduTeacher.setId(id);
        eduTeacher.setGmtCreate(LocalDateTime.now());
        eduTeacher.setGmtModified(LocalDateTime.now());
        eduTeacher.setStatus("0");
        int insert = teacherMapper.insert(eduTeacher);
        TbCheckTeacher tbCheckTeacher = new TbCheckTeacher();
        tbCheckTeacher.setTeacherId(id);
        tbCheckTeacher.setCheckStatus("2");
        int insert1 = tbCheckTeacherMapper.insert(tbCheckTeacher);
        if (insert == 0 || insert1 == 0){
            return "-1";
        }
        return "-1";
    }


}
